package com.study.talk.app.group;

import android.content.Intent;
import android.os.Bundle;


import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;


import com.study.talk.adapter.GroupListAdapter;
import com.study.talk.databinding.ActivityAddGroupBinding;
import com.study.talk.entity.GroupInfo;
import com.study.talk.entity.resp.QueryGroupInfoListResponse;
import com.study.talk.net.OkHttp;
import com.study.talk.util.GsonInstance;
import com.study.talk.util.ToastUtil;

import java.util.List;

import io.netty.util.internal.StringUtil;

public class AddGroupActivity extends AppCompatActivity {


    private List<GroupInfo> groupInfoList;

    private ActivityAddGroupBinding activityAddGroupBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityAddGroupBinding = ActivityAddGroupBinding.inflate(getLayoutInflater());
        setContentView(activityAddGroupBinding.getRoot());
        setSupportActionBar(activityAddGroupBinding.addGroupToolbar);
        initView();
    }

    private void initView() {

        activityAddGroupBinding.addGroupToolbar.setNavigationOnClickListener(v -> finish());

        activityAddGroupBinding.addGroupSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (StringUtil.isNullOrEmpty(query)){
                    ToastUtil.showToast(AddGroupActivity.this,"请输入搜索内容");
                    return false;
                }
                OkHttp.get("/groupInfo/get/list/"+query, res -> {
                    QueryGroupInfoListResponse response = GsonInstance.instance.fromJson(res, QueryGroupInfoListResponse.class);
                    if (response.getStatus() == 200){
                        if (response.getData() == null || response.getData().isEmpty()) {
                            runOnUiThread(()-> ToastUtil.showToast(AddGroupActivity.this,"暂无符合条件的群聊..."));
                        }else {
                            groupInfoList = response.getData();
                            GroupListAdapter adapter = new GroupListAdapter(groupInfoList);
                            runOnUiThread(()->{
                                activityAddGroupBinding.addGroupListview.setAdapter(adapter);
                                activityAddGroupBinding.addGroupListview.setOnItemClickListener((adapterView, view, i, l) -> {
                                    Intent intent = new Intent(AddGroupActivity.this, GroupInfoActivity.class);
                                    intent.putExtra("data",GsonInstance.instance.toJson(groupInfoList.get(i)));
                                    startActivity(intent);
                                });
                            });
                        }
                    }
                });
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }
}